package com.liyou.full.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.liyou.full.common.CustomException;
import com.liyou.full.mapper.CategoryMapper;
import com.liyou.full.pojo.Category;
import com.liyou.full.pojo.Dish;
import com.liyou.full.pojo.Setmeal;
import com.liyou.full.service.CategoryService;
import com.liyou.full.service.DishService;
import com.liyou.full.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService {

    @Autowired
    private DishService dishService;

    @Autowired
    private SetmealService setmealService;

    /**
     * 根据id删除分类
     * @param id
     */
    @Override
    public void remove(Long id){

        //添加查询条件，根据id分类进行查询
        LambdaQueryWrapper<Dish> dishLambdaQueryWrapper = new LambdaQueryWrapper<>();
        dishLambdaQueryWrapper.eq(Dish::getCategoryId,id);
        int count1 = (int) dishService.count(dishLambdaQueryWrapper);

        //判断当前分类是否关联了套餐
        if (count1 > 0){
            //已经关联套餐，抛出业务异常
            throw new CustomException("该菜品已经关联了套餐，无法删除");
        }

        LambdaQueryWrapper<Setmeal> setmealLambdaQueryWrapper = new LambdaQueryWrapper<>();
        setmealLambdaQueryWrapper.eq(Setmeal::getCategoryId, id);
        int count2 = (int) setmealService.count(setmealLambdaQueryWrapper);

        //判断当前分类是否关联了菜品
        if (count2 > 0){
            //已经关联菜品，抛出业务异常
            throw new CustomException("该套餐已经关联了菜品，无法删除");
        }

        //正常删除
        super.removeById(id);

    }


}
